package com.amap.api.col.s;

import android.content.Context;
import com.amap.api.col.s.am;
import com.amap.api.col.s.dz;
import com.amap.api.services.core.AMapException;
import com.amap.api.services.core.ServiceSettings;
import java.util.Map;

/* compiled from: BasicHandler.java */
/* loaded from: classes.dex */
public abstract class e<T, V> extends cd {
    protected T b;
    protected Context e;
    protected boolean a = true;
    protected int c = 1;
    protected String d = "";
    private int g = 1;
    protected String f = "";

    protected abstract V a(String str) throws AMapException;

    protected abstract String a_();

    @Override // com.amap.api.col.s.dz
    public final String d() {
        return "sea";
    }

    protected am.b e() {
        return null;
    }

    @Override // com.amap.api.col.s.dz
    public Map<String, String> f() {
        return null;
    }

    @Override // com.amap.api.col.s.dz
    public Map<String, String> g() {
        return null;
    }

    public e(Context context, T t) {
        a(context, t);
    }

    private void a(Context context, T t) {
        this.e = context;
        this.b = t;
        this.c = 1;
        b(ServiceSettings.getInstance().getSoTimeOut());
        a(ServiceSettings.getInstance().getConnectionTimeOut());
    }

    private String j() {
        return this.f;
    }

    private String z() {
        String a = a();
        if (a != null) {
            try {
                int indexOf = a.indexOf(".com/");
                int indexOf2 = a.indexOf("?");
                if (indexOf2 == -1) {
                    return a.substring(indexOf + 5);
                }
                return a.substring(indexOf + 5, indexOf2);
            } catch (Throwable unused) {
                return null;
            }
        }
        return null;
    }

    protected V a(byte[] bArr) throws AMapException {
        String str;
        try {
            str = new String(bArr, "utf-8");
        } catch (Exception e) {
            n.a(e, "ProtocalHandler", "loadData");
            str = null;
        }
        if (str == null || str.equals("")) {
            return null;
        }
        n.b(str);
        return a(str);
    }

    public final V c() throws AMapException {
        if (this.b != null) {
            try {
                return A();
            } catch (AMapException e) {
                aw.a(z(), j(), e);
                throw e;
            }
        }
        return null;
    }

    private V A() throws AMapException {
        Object obj;
        am amVar;
        am.c a;
        try {
            am.b e = e();
            boolean b = am.a().b(e);
            boolean z = false;
            int i = 0;
            V v = null;
            boolean z2 = false;
            while (i < this.c) {
                long currentTimeMillis = System.currentTimeMillis();
                try {
                    try {
                        try {
                            int protocol = ServiceSettings.getInstance().getProtocol();
                            cc.a().a(this.e);
                            dy b2 = dy.b();
                            if (b && (a = am.a().a(e)) != null && a.a != null) {
                                v = (V) a.a;
                                try {
                                    aw.a(this.e, e.a, a.b);
                                    z2 = true;
                                } catch (bv e2) {
                                    e = e2;
                                    z2 = true;
                                    aw.a(this.e, z(), System.currentTimeMillis() - currentTimeMillis, z);
                                    i++;
                                    if (i >= this.c) {
                                        if (!"http连接失败 - ConnectionException".equals(e.getMessage()) && !"socket 连接异常 - SocketException".equals(e.getMessage()) && !"未知的错误".equals(e.a()) && !"服务器连接失败 - UnknownServiceException".equals(e.getMessage())) {
                                            throw new AMapException(e.a(), 1, e.c());
                                        }
                                        throw new AMapException(AMapException.AMAP_CLIENT_NETWORK_EXCEPTION, 1, e.c());
                                    }
                                    try {
                                        Thread.sleep(this.g * 1000);
                                        if (b && !z2) {
                                            am.a().a(e, v);
                                        }
                                        z = false;
                                    } catch (InterruptedException unused) {
                                        if (!"http连接失败 - ConnectionException".equals(e.getMessage()) && !"socket 连接异常 - SocketException".equals(e.getMessage()) && !"服务器连接失败 - UnknownServiceException".equals(e.getMessage())) {
                                            throw new AMapException(e.a(), 1, e.c());
                                        }
                                        throw new AMapException(AMapException.AMAP_CLIENT_NETWORK_EXCEPTION, 1, e.c());
                                    }
                                } catch (AMapException e3) {
                                    e = e3;
                                    z2 = true;
                                    aw.a(this.e, z(), System.currentTimeMillis() - currentTimeMillis, z);
                                    i++;
                                    if (i >= this.c) {
                                        throw e;
                                    }
                                    if (b && !z2) {
                                        amVar = am.a();
                                        amVar.a(e, v);
                                    }
                                    z = false;
                                } catch (Throwable th) {
                                    th = th;
                                    obj = v;
                                    z2 = true;
                                    if (b) {
                                        am.a().a(e, obj);
                                    }
                                    throw th;
                                }
                            }
                            if (v == null) {
                                byte[] a2 = a(protocol, b2, this);
                                long currentTimeMillis2 = System.currentTimeMillis();
                                v = b(a2);
                                aw.a(this.e, z(), currentTimeMillis2 - currentTimeMillis, true);
                            }
                            i = this.c;
                        } catch (bv e4) {
                            e = e4;
                        } catch (AMapException e5) {
                            e = e5;
                        }
                        if (b && !z2) {
                            amVar = am.a();
                            amVar.a(e, v);
                        }
                        z = false;
                    } catch (Throwable th2) {
                        th = th2;
                        obj = v;
                        if (b && !z2) {
                            am.a().a(e, obj);
                        }
                        throw th;
                    }
                } catch (Throwable th3) {
                    th = th3;
                    obj = null;
                }
            }
            return v;
        } catch (AMapException e6) {
            throw e6;
        } catch (Throwable th4) {
            th4.printStackTrace();
            throw new AMapException(AMapException.AMAP_CLIENT_UNKNOWN_ERROR);
        }
    }

    private byte[] a(int i, dy dyVar, cd cdVar) throws bv {
        ea e;
        a(i == 1 ? dz.c.HTTP : dz.c.HTTPS);
        if (this.a) {
            e = dy.a(cdVar);
        } else {
            e = dy.e(cdVar);
        }
        if (e != null) {
            byte[] bArr = e.a;
            this.f = e.d;
            return bArr;
        }
        return null;
    }

    private V b(byte[] bArr) throws AMapException {
        return a(bArr);
    }
}
